package com.bean;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.tools.WXDatahubConfig;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

public class DTO2DataConverter {

    public SignsDataBO convert(@NotNull SlDataDTO dto){
        String detail = dto.getDetail();
        List<TimeDimensionData> timeDimensionData = generateTimeData(detail);


        return SignsDataBO.createSlTO(dto.getCurDeep(),dto.getName(),dto.getTimes()).
        idxTimeDimensionData(timeDimensionData)
        .build();
    }



    public List<TimeDimensionData> generateTimeData(String timeDetail){
//        [{"time":"2024-08-22 10:15:00","val":1710.22000},{"time":"2024-08-22 10:05:00","val":1710.20000},{"time":"2024-08-22 09:55:00","val":1710.20000},{"time":"2024-08-22 09:45:00","val":1710.19000},{"time":"2024-08-22 09:35:00","val":1710.18000},{"time":"2024-08-22 09:25:00","val":1710.20000}]
        List<TimeDTO> list = JSON.parseObject(timeDetail, new TypeReference<List<TimeDTO>>(){});
        /**
         * [{"time":"2024-08-22 10:15:00","val":1710.22000},
         * {"time":"2024-08-22 10:05:00","val":1710.20000},
         * {"time":"2024-08-22 09:55:00","val":1710.20000},
         * {"time":"2024-08-22 09:45:00","val":1710.19000},
         * {"time":"2024-08-22 09:35:00","val":1710.18000},
         * {"time":"2024-08-22 09:25:00","val":1710.20000}]
         */
        List<TimeDimensionData> result = new ArrayList<>();
        for (int i=0;i<list.size();i++) {
            TimeDimensionData t = timeDTO2TimeDimensionData(list.get(i));
            t.setId(i+"");
            result.add(t);
        }
        return result;
    }


    private TimeDimensionData timeDTO2TimeDimensionData(TimeDTO timeDTO){
        TimeDimensionData data = new TimeDimensionData();
        String timeValue = timeDTO.getVal();
        if(timeValue.length()>7){
                timeValue = timeValue.substring(0,7);
        }else if(timeValue.length()<7){
                timeValue = timeValue+"0";
        }
        data.setTimeValue(timeValue);
        data.setZoningCode(WXDatahubConfig.zoningCode);
        data.setParentZoningCode(WXDatahubConfig.pareZoningCode);
        data.setZoningName(WXDatahubConfig.zoningName);
        data.setTimeDim("hour");
        data.setStatisticsTime(timeDTO.getTime());
        return data;
    }
}
